package com.j180.controller.service;

import com.j180.bean.PageBean;
import com.j180.bean.ResultBean;
import com.j180.bean.Service;
import com.j180.service.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ser")
public class ServiceController {

        @Autowired
        private IService iService;


    @GetMapping("/showAll/{stId}/{curPage}/{pageSize}")
        public ResultBean<PageBean<Service>> listByStId(
                @PathVariable("stId") int stId,
                @PathVariable("curPage") int curPage,
                @PathVariable("pageSize") int pageSize) {
            ResultBean<PageBean<Service>> resultBean = new ResultBean<>();
            PageBean<Service> services = iService.showServices(stId,curPage,pageSize);
            if(services != null){
                resultBean.setT(services);
                resultBean.setCode(1);
                resultBean.setMsg("操作成功");
            }else {
                resultBean.setCode(0);
                resultBean.setMsg("操作失败");
            }
            return resultBean;
        }

    @GetMapping("/service/get/{stId}/{seId}")
    public ResultBean<Service> getByStIdAndSeId(@PathVariable Integer stId, @PathVariable Integer seId) {
       Service service = iService.showStoreServiceById(seId,stId);
       ResultBean<Service>resultBean = new ResultBean<>();
        if(service != null){
            resultBean.setCode(1);
            resultBean.setT(service);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }
        return resultBean;
    }

        @PostMapping("/add")
        public ResultBean<Integer> addService(@RequestBody(required = false) Service service) {
            Integer integer = iService.addService(service);
            ResultBean<Integer> resultBean = new ResultBean<>();
            resultBean.setT(integer);
            resultBean.setCode(integer);
            resultBean.setMsg(integer>0?"操作成功":"操作失败");
            return resultBean;
        }

        @PostMapping("/update")
        public ResultBean<Integer> updateService(@RequestBody(required = false) Service service) {
            Integer integer = iService.updateService(service);
            ResultBean<Integer> resultBean = new ResultBean<>();
            resultBean.setT(integer);
            resultBean.setCode(integer);
            resultBean.setMsg(integer>0?"操作成功":"操作失败");
            return resultBean;
        }

        @DeleteMapping("/del/{seId}")
        public ResultBean<Integer> delBySeId(@PathVariable Integer seId) {
            Integer integer = iService.deleteService(seId);
            ResultBean<Integer> resultBean = new ResultBean<>();
            resultBean.setT(integer);
            resultBean.setCode(integer);
            resultBean.setMsg(integer>0?"操作成功":"操作失败");
            return resultBean;
        }
    }

